//
//  WayModelPickerView.m
//  WayService
//
//  Created by WillyZhao on 16/8/11.
//  Copyright © 2016年 Johnny. All rights reserved.
//

#import "WayModelPickerView.h"

@implementation WayModelPickerView {
    NSArray *dayarr1;
    NSArray *dayarr2;
    NSMutableArray *yeararr;
    UIPickerView *picker;
    NSArray *dataSource;
    UIView *maskView;
}
@synthesize year,day,month;

-(instancetype)initWithFrame:(CGRect)frame AndDataSource:(NSArray *)array WithStyle:(WayModelPickerViewStyle)style {
    if (self = [super initWithFrame:frame]) {
        //装入数据源
        dataSource = [NSArray arrayWithArray:array];
        self.style = style;
        
        //判断初始化类型
        picker = [[UIPickerView alloc] initWithFrame:self.bounds];
        
        picker.delegate  = self;
        picker.dataSource = self;
        picker.showsSelectionIndicator = YES;
        [self addSubview:picker];
       
        if (self.style==WayModelPickerViewOneSection) {
            NSLog(@"一个");
        }else{
            dayarr1 = [NSArray arrayWithObjects:@"31",@"28",@"31",@"30",@"31",@"30",@"31",@"31",@"30",@"31",@"30",@"31", nil];
            dayarr2 = [NSArray arrayWithObjects:@"31",@"29",@"31",@"30",@"31",@"30",@"31",@"31",@"30",@"31",@"30",@"31", nil];
            yeararr = [[NSMutableArray alloc] initWithCapacity:0];
            NSDate *date = [NSDate date];
            NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
            [formatter setDateFormat:@"yyyy"];
            year = [[formatter stringFromDate:date] intValue];
            [formatter setDateFormat:@"MM"];
            month = [[formatter stringFromDate:date] intValue];
            [formatter setDateFormat:@"dd"];
            day = [[formatter stringFromDate:date] intValue];
            
            for (int i = year-60; i<year+60; i++) {
                NSString *str = [NSString stringWithFormat:@"%d",i];
                NSLog(@"%@",str);
                [yeararr addObject:str];
            }
            [picker selectRow:60 inComponent:0 animated:YES];
            [picker selectRow:month-1 inComponent:1 animated:YES];
            [picker selectRow:day-1 inComponent:2 animated:YES];
        }
       
        
    }
    return self;
}

#pragma mark pickerview
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    if (self.style==WayModelPickerViewOneSection) {
        return 1;
    }else{
        return 5;
    }
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (self.style==WayModelPickerViewOneSection) {
        return dataSource.count;
    }else{
        if (component == 0) {
            return  yeararr.count;
        }else if (component == 1) {
            return 12;
        }else if (component == 2) {
            //获得前二个滚轮的当前所选行的索引
            int row = (int)[picker selectedRowInComponent:0];
            int nowyear = [[yeararr objectAtIndex:row] intValue];
            int nowmonth = (int)[picker selectedRowInComponent:1];
            if ((nowyear % 4 == 0 && nowyear % 100 !=0 )||(nowyear % 400 == 0)) {
                return [[dayarr2 objectAtIndex:nowmonth] intValue];
            }
            else
            {
                return [[dayarr1 objectAtIndex:nowmonth] intValue];
            }
        }else if (component == 3) {
            return 24;
        }else if (component == 4) {
            return 60;
        }else{
            return 10000;
        }
    }
}

-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    
    UILabel *mycom1 = [[UILabel alloc] init];
    mycom1.textAlignment = NSTextAlignmentCenter;
    mycom1.backgroundColor = [UIColor clearColor];
    mycom1.frame = CGRectMake(0, 0, self.frame.size.width/3.0, 50);
    [mycom1 setFont:[UIFont systemFontOfSize:15]];
    
    if (self.style==WayModelPickerViewOneSection) {
        mycom1.text = dataSource[row];
        return mycom1;
    }else{
        if(component == 0)
        {
            
            mycom1.text = [NSString stringWithFormat:@"%@年",[yeararr objectAtIndex:row]];
            
        }
        else if(component == 1){
            NSInteger index= row+1;
            if (index<10) {
                mycom1.text = [NSString stringWithFormat:@"0%ld月",index];
            }else{
                mycom1.text = [NSString stringWithFormat:@"%ld月",index];
            }
            
            
        }else if(component == 2){
            NSInteger index= row+1;
            if (index<10) {
                mycom1.text = [NSString stringWithFormat:@"0%ld日",index];
            }else{
                mycom1.text = [NSString stringWithFormat:@"%ld日",index];
            }
        
            
        }else if (component == 3) {
            mycom1.text = [NSString stringWithFormat:@"%ld点",row];
        }else{
            mycom1.text = [NSString stringWithFormat:@"%ld分",row];
        }
        return mycom1;
    }
}

-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
    if (self.style==WayModelPickerViewOneSection) {
        return  self.frame.size.width;
    }else{
        return self.frame.size.width/5;
    }
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (self.style==WayModelPickerViewOneSection) {
//        NSInteger index = row + 1;
        self.block([NSString stringWithFormat:@"%@",dataSource[row]]);
    }else{
        int rowy = (int)[picker selectedRowInComponent:0];
        int rowm = (int)[picker selectedRowInComponent:1];
        int rowd = (int)[picker selectedRowInComponent:2];
        year = [[yeararr objectAtIndex:rowy] intValue];
        month = (int)rowm+1;
        day = (int)rowd+1;
        
        NSInteger hh = [picker selectedRowInComponent:3];
  
        NSString * str_hh = [NSString stringWithFormat:@"%ld点",hh];
        
        NSInteger mm = [picker selectedRowInComponent:4];

        NSString * str_mm = [NSString stringWithFormat:@"%ld分",mm];
        
        NSString *str = [NSString stringWithFormat:@"%d年%d月%d日 %@:%@",year,month,day,str_hh,str_mm];
        self.block(str);

    }
}

-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return self.bounds.size.height/6;
}

-(void)show {
    [self animationWithView:self duration:.3f];
    
    maskView.alpha = 0;
    
    [UIView animateWithDuration:.25f animations:^{
        maskView.alpha = 0.5;
    }];
    
    [[UIApplication sharedApplication].keyWindow addSubview:maskView];
    [[UIApplication sharedApplication].keyWindow addSubview:self];
}

- (void)animationWithView:(UIView *)view duration:(CFTimeInterval)duration{
    CAKeyframeAnimation * animation;
    animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    animation.duration = duration;
    animation.removedOnCompletion = NO;
    
    animation.fillMode = kCAFillModeForwards;
    
    NSMutableArray *values = [NSMutableArray array];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.2, 1.2, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
    
    animation.values = values;
    animation.timingFunction = [CAMediaTimingFunction functionWithName: @"easeInEaseOut"];
    
    [view.layer addAnimation:animation forKey:nil];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
